package com.bi.bitest.bao.adapters;

/**
 * Created
 * Authour: 雷佳
 * Date: 15-10-21
 */

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bi.bitest.bao.R;
import com.bi.bitest.bao.entity.discoverrecommend.*;
import com.bi.bitest.bao.widgets.ColumnsItemView;
import com.bi.bitest.bao.widgets.SpecialItemView;
import com.squareup.picasso.Picasso;

import java.lang.reflect.Field;
import java.util.List;

/**
 * 发现 推荐部分的 ListView Adapter
 *
 * 支持多布局的复用
 */
public class DiscoverRecommendAdapter extends BaseAdapter{
    private Context context;

    private List<DiscoverRecommendItem> items;
    //点击事件处理器
    //推荐专辑点击事件
    private View.OnClickListener onrRecommendAlbumClickListener;

    public void setOnrRecommendAlbumClickListener(View.OnClickListener onrRecommendAlbumClickListener) {
        this.onrRecommendAlbumClickListener = onrRecommendAlbumClickListener;
    }



    public DiscoverRecommendAdapter(Context context, List<DiscoverRecommendItem> items) {
        this.context = context;
        this.items = items;
    }

    @Override
    public int getCount() {
        int ret = 0;

        if(items!= null)
            ret = items.size();

        return ret;
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 3;   //  当前ListView 可以显示三种布局类型，小编推荐（热门推荐),精品推荐，发现
    }

    @Override
    public int getItemViewType(int position) {
        int ret = 0;
        DiscoverRecommendItem item = items.get(position);
        if(item instanceof DiscoverRecommendAlbums){// 小编推荐，热门推荐
            ret = 0;
        }else if(item instanceof DiscoverRecommendSpecial){//精品听单
            ret = 1;
        }else if(item instanceof DiscoverRecommendColumns){//发现新奇
            ret = 2;
        }

        return ret;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret = null;
        // 1.获取数据
        DiscoverRecommendItem item = items.get(position);

        if(item instanceof DiscoverRecommendAlbums){// 小编推荐，热门推荐
            ret = bindAlbumsView(position,convertView,parent);
        }else if(item instanceof DiscoverRecommendSpecial){//精品听单
            ret = bindSpecialView(position, convertView, parent);
        }else if(item instanceof DiscoverRecommendColumns){//发现新奇
            ret= bindColumnsView(position, convertView, parent);
        }

        return ret;
    }

    /**
     * 加载 专辑推荐的 Item：小编推荐和热门推荐
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    private View bindAlbumsView(int position, View convertView, ViewGroup parent){
        View ret = null;

        if(convertView != null){
            ret = convertView;
        }else{
            ret = LayoutInflater.from(context).inflate(
                    R.layout.discover_recommend_item_album,parent,false
            );
        }

        //2
        AlbumViewHolder holder = (AlbumViewHolder) ret.getTag();
        if(holder == null){

            holder = new AlbumViewHolder();

            holder.txtTitle = (TextView) ret.findViewById(R.id.recommend_album_title);
            holder.txtMore = (TextView) ret.findViewById(R.id.recommend_album_more);

            //TODO 点击more 进入推荐列表

            holder.albumsIcons = new ImageView[3];
            holder.albumsNames = new TextView[3];
            holder.trackNames = new TextView[3];

            for (int i = 0; i <3; i++) {

                holder.albumsIcons[i] = (ImageView) findView(ret,"recommend_album_ic"+i);
                holder.albumsIcons[i].setOnClickListener(onrRecommendAlbumClickListener);

                //TODO点击专辑图片，进入专辑详情
                holder.albumsNames[i] = (TextView) findView(ret,"recommend_album_name_"+i);

                holder.trackNames[i] = (TextView) findView(ret,"recommend_album_track_name_"+i);
            }

            ret.setTag(holder);

        }

        //3.
        DiscoverRecommendAlbums albums = (DiscoverRecommendAlbums) items.get(position);

        String title = albums.getTitle();
        holder.txtTitle.setText(title);

        //处“更多“
        if(albums.isHasMore())
            holder.txtMore.setVisibility(View.VISIBLE);
        else holder.txtMore.setVisibility(View.INVISIBLE);

        //处理专辑
        List<AlbumRecommend>albumRecommends=albums.getAlbumRecommends();
              int len=holder.albumsIcons.length;
        if(albumRecommends!=null){

        for(int i=0;i<len;i++){
           AlbumRecommend albumRecommend=albumRecommends.get(i);
            title=albumRecommend.getTitle();
            holder.albumsNames[i].setText(title);
            title=albumRecommend.getTrackTitle();
            holder.trackNames[i].setText(title);

            //picasso 图片加载
            String coverLarge=albumRecommend.getCoverLarge();
            ImageView imageView=holder.albumsIcons[i];
            imageView.setTag(albumRecommend);

            Picasso.with(context)
                    .load(coverLarge)
                    .into(holder.albumsIcons[i]);
            }

        }
        return ret;
    }

    /**
     * 加载 精品听单
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    private View bindSpecialView(int position, View convertView, ViewGroup parent){
        View ret = null;
        //1.复用
        if(convertView!=null){
            ret=convertView;
        }else{
           ret= LayoutInflater.from(context).inflate(R.layout.discover_recommend_special_item
            ,parent,false);
        }
        //2 ViewHolder
        SpecialViewHolder holder= (SpecialViewHolder) ret.getTag();
        if(holder==null){
            holder=new SpecialViewHolder();
            holder.txtTitle= (TextView) ret.findViewById(R.id.recommend_special_title);
            holder.txtMore= (TextView) ret.findViewById(R.id.recommend_special_more);
         holder.itemContainer= (LinearLayout) ret.findViewById(R.id.recommend_special_container);

            ret.setTag(holder);
            }
        //3
        DiscoverRecommendSpecial special= (DiscoverRecommendSpecial) items.get(position);
        holder.txtTitle.setText(special.getTitle());
        if(special.isHasMore())
            holder.txtMore.setVisibility(View.VISIBLE);
        else holder.txtMore.setVisibility(View.INVISIBLE);

        holder.itemContainer.removeAllViews();
        List<SpecialItem> specialItems=special.getSpecialItems();
                if(specialItems!=null){
                    int index=-1;
                    int itemcount=specialItems.size();
                    for(SpecialItem specialItem:specialItems){
                        index++;
                        SpecialItemView itemView=new SpecialItemView(context);
                       LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
                       , ViewGroup.LayoutParams.WRAP_CONTENT);
                        itemView.setLayoutParams(lp);
                        holder.itemContainer.addView(itemView);//---------------增加一条条布局
                        //设置听单内容
                        itemView.setTitle(specialItem.getTitle());
                        itemView.setSubTtile(specialItem.getSubtitle());
                        itemView.setTxtNumber(specialItem.getFootnote());

                        itemView.setShowBottomLine(true);
                        ImageView imageView=itemView.getImgIcon();
                        String coverPath=specialItem.getCoverPath();
                        Picasso.with(context).load(coverPath).into(imageView);

                        if(index==itemcount-1)itemView.setShowBottomLine(false);
                    }

                }
        return ret;
    }

    /**
     * 加载 发现
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    private View bindColumnsView(int position, View convertView, ViewGroup parent){
        View ret = null;
        //1.复用
        if(convertView!=null){
            ret=convertView;
        }else{
            ret= LayoutInflater.from(context).inflate(R.layout.discover_recommend_columns_item
                    ,parent,false);
        }
        //2 ViewHolder
        ColumnsViewHolder holder= (ColumnsViewHolder) ret.getTag();
        if(holder==null){
            holder=new ColumnsViewHolder();
            holder.txtTitle= (TextView) ret.findViewById(R.id.recommend_columns_title);
            holder.itemContainer= (LinearLayout) ret.findViewById(R.id.recommend_columns_container);

            ret.setTag(holder);
        }
        //3
        DiscoverRecommendColumns columns= (DiscoverRecommendColumns) items.get(position);
        holder.txtTitle.setText(columns.getTitle());

        holder.itemContainer.removeAllViews();
        List<ColumnsItem> columnsItems=columns.getColumnsItems();
        if(columnsItems!=null){
            int index=-1;
            int itemcount=columnsItems.size();
            for(ColumnsItem columnsItem:columnsItems){
                index++;
                ColumnsItemView itemView=new ColumnsItemView(context);
                LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
                        , ViewGroup.LayoutParams.WRAP_CONTENT);
                itemView.setLayoutParams(lp);
                holder.itemContainer.addView(itemView);//---------------增加一条条布局
                //设置听单内容
                itemView.setTitle(columnsItem.getTitle());
                itemView.setSubTtile(columnsItem.getSubtitle());

                itemView.setShowBottomLine(true);
                ImageView imageView=itemView.getImgIcon();
                String coverPath=columnsItem.getCoverPath();
                Picasso.with(context).load(coverPath).into(imageView);

                if(index==itemcount-1)itemView.setShowBottomLine(false);
            }

        }
        return ret;
    }

    /**
     * 利用类的反射，来获取指定 R.id.xxx 代表View
     * @param container
     * @param filedName
     * @return
     */
    public static View findView(View container,String filedName){
        View ret = null;

        if(container != null && filedName !=null) {
            try {
                Class<R.id> idClass = R.id.class;

                Field field = idClass.getDeclaredField(filedName);

                int id = field.getInt(idClass);

                ret = container.findViewById(id);

            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }

        return ret;
    }


    /**
     * 小编推荐和热门推荐的 ViewHolder
     */
    private static class AlbumViewHolder{
        public TextView txtTitle;
        public TextView txtMore;

        public ImageView[] albumsIcons;//专辑图片

        public TextView[] albumsNames;//专辑名称

        public TextView[] trackNames;//专辑标题

    }
    private static class SpecialViewHolder {
        public TextView txtTitle;
        public TextView txtMore;
        //存储SpecialItemView
        public LinearLayout itemContainer;

    }
    private static class ColumnsViewHolder {
        public TextView txtTitle;
        //存储SpecialItemView
        public LinearLayout itemContainer;

    }


}













